using System;
using System.Collections.Generic;
using System.Text;
using JobShopModel.Model.JobLvl;
using JobShopModel.Model.FactoryLvl;
using JobShopModel.Model.Station;
using JobShopModel.Model;

namespace JobShopModel.Events
{
    /// <summary>
    /// Event causing the creation or arrival to the factory.
    /// 
    /// It is excecuted by the IOStation
    /// </summary>
    public class CreateJob : StationEvent
    {

        /// <summary>
        /// Ordering time
        /// </summary>
        Time orderingTime;

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="sTime">time the event is raised</param>
        /// 
        public CreateJob(Time sTime)
            : base(sTime, StationsName.IOSTATION)
        {
            this.orderingTime = sTime;
        }

        /// <summary>
        /// Polymorphic method
        /// It will be instanced by all Events and excecuted by the eventloop
        /// </summary>
        public override void Process()
        {
            Factory factory = Factory.getInstance();
            Time elapsedTime = new Time();

            //Get the station to do the job
            StationIO st = (StationIO)factory.Stations[StationsName.IOSTATION];

            //Create the job we need randomly
            Job createdJob = null;
            Random rand = new Random();
            double tmprand;
            tmprand = rand.NextDouble();

            //Conditional statements to determine (using random number) which type of Job
            //Job 1 30%, Job 2 50%, Job 3 20%
            if (tmprand <= .3)
            {
                createdJob = Job.JobFactory(JobTypes.JOB1);
                factory.JobStatistics.JobCreatedCounter[JobTypes.JOB1]++;
            }
            else if (tmprand <= .8)
            {
                createdJob = Job.JobFactory(JobTypes.JOB2);
                factory.JobStatistics.JobCreatedCounter[JobTypes.JOB2]++;
            }
            else
            {
                createdJob = Job.JobFactory(JobTypes.JOB3);
                factory.JobStatistics.JobCreatedCounter[JobTypes.JOB3]++;
            }

            //Add logs
            Logger.InsertElement(new LogItem(ItemPriorities.URGENT, factory.CurrentTime,createdJob + " has been created"));

            //Add the job to the FIFO list
            factory.Transporter.AddJob(createdJob);

            //Raise an event to call the transporter
            RequestTransport transportRequest = new RequestTransport(factory.CurrentTime + elapsedTime);
            factory.EventQueue.insertEvent(transportRequest);
            Logger.InsertElement(new LogItem(ItemPriorities.SLOW, factory.CurrentTime, OperatingStation + ", has requested the transporter"));
            Logger.InsertElement(new LogItem(ItemPriorities.CREATION,factory.CurrentTime,OperatingStation+", inserted: \""+transportRequest+"\""));

            //Set the station to blocked....
            //st.State = StationsStates;

           
        }

        /// <summary>
        /// Comparison operator (for IComparable)
        /// </summary>
        /// <param name="event1">First event</param>
        /// <param name="event2">Second event</param>
        /// <returns>Returns -1 if event1 occurs first, 1 if event2 occurs first, 0 if the times are the same</returns>
        public override int CompareTo(IEvent event1, IEvent event2)
        {
            if (event1.ExecTime < event2.ExecTime)
                return -1;
            else if (event1.ExecTime > event2.ExecTime)
                return 1;
            else
                return 0;
        }

        public override string ToString()
        {
            return "CreateJob, execTime: "+ExecTime;
        }
    }
}
